package com.tsky.system.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import com.tsky.casing.QueryBean;
import com.tsky.common.BeanUtil;
import com.tsky.system.entity.Constant;
import com.tsky.system.entity.Menu;
import com.tsky.system.entity.Role;
import com.tsky.system.entity.User;

/**
 * 工具类：Json数据格式化
 * @author: cainiao
 * @version 1.0 
 * @time: 2010-3-10 上午06:03:36
 */
public final class JsonFormat {
	
	/**
	 * 获取分页总页数
	 * @author: cainiao
	 * @time: 2010-8-26 上午02:10:37
	 * @function: 
	 * @param resultList
	 * @return
	 */
	public static int getPages(QueryBean resultList){
		int total = (int) resultList.getTotal();
		int size = resultList.getRows();
		
		int pages = 0;
		if(total <= size ){
			pages = 1;
		}else if(total > size){
			pages = total/size;
			if(total%size>0){
				pages += 1;
			}
		}
		
		return pages;
	}
	
	public static String userRoleToJson(List resultList, String roleid) {
		List mapList = new ArrayList();
		Map selMap = new HashMap();
		selMap.put("id", "");
		if(BeanUtil.isEmpty(roleid)){
			selMap.put("selected", true);
		}
		
		mapList.add(selMap);
		for (Object object : resultList) {
			Role entity = (Role) object;
			Map cellMap = new HashMap();
			
			String id = entity.getId();
			if(id.equals(roleid)){
				cellMap.put("selected", "true");	
			}
			cellMap.put("id", id);
			cellMap.put("name", entity.getName());
			
			mapList.add(cellMap);
		}

		// Object object = JSONArray.fromObject(mapList);
		String object = null;
		try {
			object = JSONUtil.serialize(mapList);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}
	
	public static String userSexToJson(String sex) {
		List mapList = new ArrayList();
		Map selMap = new HashMap();
		selMap.put("id", "");
		if(BeanUtil.isEmpty(sex)){
			selMap.put("selected", true);
		}
		mapList.add(selMap);
		Map cellMapMale = new HashMap();
		if("1".equals(sex)){
			cellMapMale.put("selected", "true");	
		}
		cellMapMale.put("value", 1);
		cellMapMale.put("name", "男");
		
		Map cellMapFemale = new HashMap();
		if("0".equals(sex)){
			cellMapFemale.put("selected", "true");	
		}
		cellMapFemale.put("value", 0);
		cellMapFemale.put("name", "女");
		
		mapList.add(cellMapMale);
		mapList.add(cellMapFemale);

		// Object object = JSONArray.fromObject(mapList);
		// return object.toString();
		String object = null;
		try {
			object = JSONUtil.serialize(mapList);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return object;
	}
	
	/**
	 * 菜单集合格式化为JSON数据
	 * @param roleMenus
	 * @return
	 */
	public static String getMenuTree(List roleMenus) {
		List mapList = new ArrayList();// JSON集合
		Map cellMap;
		List childList;
		
		for (Object menuInfos : roleMenus) {
			Object[] menu = (Object[]) menuInfos;
			if(menu[4] == null){// pid = null
				cellMap = new TreeMap();
				if("true".equals(menu[5]) && "0".equals(menu[3])){
					cellMap.put("checked", "true");
				}
				cellMap.put("id", menu[0]);
				cellMap.put("text", menu[1]);
				cellMap.put("attributes", menu[2]);
				cellMap.put("url", menu[2]);
				cellMap.put("icon", menu[6]);
				
				getChildren(cellMap, menu, roleMenus);
				
				mapList.add(cellMap);
			}
		}
		
		String object = null;
		try {
			object = JSONUtil.serialize(mapList);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}
	
	/**
	 * 获取子菜单
	 * @param cellMap
	 * @param pMenu
	 * @param roleMenus
	 */
	private static void getChildren(Map cellMap, Object[] pMenu, List roleMenus) {
		List childList = new ArrayList();
		Map childMap;
		
		Object pid = pMenu[0];
		for (Object menuInfos : roleMenus) {
			Object[] menu = (Object[]) menuInfos;
			if(pid.equals(menu[4])){
				childMap = new TreeMap();
				if("true".equals(menu[5])){
					childMap.put("checked", "true");
				}
				childMap.put("id", menu[0]);
				childMap.put("text", menu[1]);
				childMap.put("attributes", menu[2]);
				childMap.put("url", menu[2]);
				childMap.put("icon", menu[6]);
				
				getChildren(childMap, menu, roleMenus);
				
				childList.add(childMap);
			}
		}
		
		if("1".equals(pMenu[3].toString())){// 非叶子节点
			cellMap.put("state", "closed");
			cellMap.put("children", childList);
		}
	}
	
	/**
	 * User实体对象格式化为JSON数据
	 * @param entityList
	 * @param page
	 * @param total
	 * @return
	 */
	public static String userToJson(QueryBean resultList) {
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			User entity = (User) object;
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("code", entity.getCode());
			cellMap.put("name", entity.getName());
			cellMap.put("sex", "0".equals(entity.getSex())? "女" : "男");
			cellMap.put("mobile", entity.getMobile());
			cellMap.put("phone", entity.getPhone());
			cellMap.put("email", entity.getEmail());
			cellMap.put("status", "0".equals(entity.getStatus())? "停用" : "在用");
			cellMap.put("createTime", BeanUtil.formatToDate(entity.getCreateTime()));
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
		// Object object = JSONObject.fromObject(pageInfo);
		// return object.toString();
	}
	
	/**
	 * Role实体对象格式化为JSON数据
	 * @author: cainiao
	 * @time: 2010-5-24 上午07:12:11
	 * @function: 
	 * @param resultList
	 * @return
	 */
	public static String roleToJson(QueryBean resultList) {
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Role entity = (Role) object;
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("name", entity.getName());
			cellMap.put("description", entity.getDescription());
			cellMap.put("status", "0".equals(entity.getStatus())? "停用" : "在用");
			cellMap.put("createTime", BeanUtil.formatToDate(entity.getCreateTime()));
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
		//Object object =JSONObject.fromObject(pageInfo);
		//return object.toString();
	}
	
	/**
	 * Menu实体对象格式化为JSON数据
	 * @author: cainiao
	 * @time: 2010-5-24 上午07:12:20
	 * @function: 
	 * @param resultList
	 * @return
	 */
	public static String menuToJson(QueryBean resultList) {
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Menu entity = (Menu) object;
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("name", entity.getName());
			cellMap.put("url", entity.getUrl());
			Menu pmenu = entity.getParent();
			String pName = "无";
			if(!BeanUtil.isEmpty(pmenu)){
				pName = pmenu.getName();
			}
			cellMap.put("parent", pName);
			cellMap.put("status", "0".equals(entity.getStatus())? "停用" : "在用");
			cellMap.put("createTime", BeanUtil.formatToDate(entity.getCreateTime()));
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
		//Object object = JSONObject.fromObject(pageInfo);
		//return object.toString();
	}
	
	public static String getTypeTree(List roleMenus) {
		//m.id, m.name, m.url, m.property, m.pid, m.orderNO, m.icon
		//m.id, m.name, m.property, m.pid
		List mapList = new ArrayList();// JSON集合
		Map cellMap;
		List childList;
		
		for (Object menuInfos : roleMenus) {
			Object[] menu = (Object[]) menuInfos;
			if(menu[3] == null){// pid = null
				cellMap = new TreeMap();
				cellMap.put("id", menu[0]);
				cellMap.put("text", menu[1]);
				
				getTypeChildren(cellMap, menu, roleMenus);
				
				mapList.add(cellMap);
			}
		}
		
		String object = null;
		try {
			object = JSONUtil.serialize(mapList);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
		// return JSONArray.fromObject(mapList).toString();
	}
	
	/**
	 * 获取子菜单
	 * @param cellMap
	 * @param pMenu
	 * @param roleMenus
	 */
	private static void getTypeChildren(Map cellMap, Object[] pMenu, List roleMenus) {
		List childList = new ArrayList();
		Map childMap;
		
		Object pid = pMenu[0];
		for (Object menuInfos : roleMenus) {
			Object[] menu = (Object[]) menuInfos;
			if(pid.equals(menu[3])){
				childMap = new TreeMap();
				childMap.put("id", menu[0]);
				childMap.put("text", menu[1]);
				
				getTypeChildren(childMap, menu, roleMenus);
				
				childList.add(childMap);
			}
		}
		
		if("1".equals(pMenu[2].toString())){// 非叶子节点
			cellMap.put("state", "closed");
			cellMap.put("children", childList);
		}
	}
	
	/**
	 * Constant实体对象格式化为JSON数据
	 * @param entityList
	 * @param page
	 * @param total
	 * @return
	 */
	public static String constantToJson(QueryBean resultList) {
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Constant entity = (Constant) object;
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("name", entity.getName());
			cellMap.put("fieldName", entity.getFieldName());
			cellMap.put("showValue", entity.getShowValue());
			cellMap.put("status", "0".equals(entity.getStatus())? "停用" : "在用");
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object;
	}
	
	public static String userListToJson(QueryBean resultList) {
		if(BeanUtil.isEmpty(resultList)
				|| BeanUtil.isEmpty(resultList.getDataList())){
			return "";
		}
		List mapList = new ArrayList();
		
		List entityList = resultList.getDataList();
		for (Object object : entityList) {
			User entity = (User) object;
			Map cellMap = new HashMap();
			
			String name = entity.getName();
			String userId = entity.getId();
			
			cellMap.put("value", userId);
			cellMap.put("name", name);
			
			mapList.add(cellMap);
		}

		String object = null;
		try {
			object = JSONUtil.serialize(mapList);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}
	
}
